Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business
Imports System.Text


Public Class UserList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents rptList As MyFramework.Web.UI.WebControls.Repeater
    Protected WithEvents crContent As MyFramework.Web.UI.WebControls.ContentRegion
    Protected WithEvents pageTitle As MyFramework.Web.UI.WebControls.ContentRegion
    Protected WithEvents Masterpage1 As MyFramework.Web.UI.WebControls.MasterPage

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents ucPager As Pager

    Private allOfficeCol As OfficeCollection
    Private belongedOfficeCol As OfficeCollection = Nothing
    Private oldUserID As Integer = -1

    Protected Function GetUserOffices(ByVal userID As Integer) As OfficeCollection
        If oldUserID <> userID Then
            belongedOfficeCol = OfficeService.GetOffices(userID)
            oldUserID = userID
        End If
        Return belongedOfficeCol
    End Function

    Protected Function GetBelongedOfficeHeaderHTML() As String
        Dim sHTML As New StringBuilder
        Dim i As Integer

        For i = 0 To allOfficeCol.Count - 1
            sHTML.AppendFormat("<td width='50px' align='center'>{0}</td>", allOfficeCol(i).OfficeName)
        Next

        Return sHTML.ToString()
    End Function

    Private Function IsOfficeSelected(ByVal userID As Integer, ByVal officeID As Integer) As Boolean
        For Each oneOffice As Office In GetUserOffices(userID)
            If oneOffice.OfficeID = officeID Then
                Return True
            End If
        Next
        Return False
    End Function

    Protected Function GetBelongedOfficeHTML(ByVal userID As Integer) As String
        Dim sHTML As New StringBuilder
        Dim i As Integer

        For i = 0 To allOfficeCol.Count - 1
            Dim checkHTML As String = ""
            If IsOfficeSelected(userID, allOfficeCol(i).OfficeID) Then
                checkHTML = "<img src='../../images/checkmark.gif' border='0' />"
            End If
            sHTML.AppendFormat("<td align='center'>{0}</td>", checkHTML)
        Next

        Return sHTML.ToString()
    End Function

    Protected Function GetUserTypeHTML(ByVal data As Object) As String
        Select Case Convert.ToInt32(data)
            Case 2
                Return "Officer"
            Case 3
                Return "Customer"
            Case 4
                Return "Sales"
            Case 5
                Return "Manager"
        End Select
    End Function

    Protected Function GetCountryHTML(ByVal data As Object) As String
        Dim countryItem As Country = (CountryService.GetCountry(Convert.ToInt32(data)))
        If (countryItem Is Nothing) Then
            Return ""
        Else
            Return String.Format("<a href=""javascript:;"" onclick=""OpenCountryView('{1}')"" >{0}</a>", countryItem.CountryName, countryItem.CountryID)
        End If
    End Function

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            allOfficeCol = OfficeService.GetOffices(OfficeColumns.OfficeID, MyFramework.OrderDirection.ASC)

            Dim totalRecords As Integer
            ucPager.PageSize = Config.PageSize
            rptList.DataSource = USERService.GetNonAdminUSERs(USERColumns.LOGIN_NAME, _
                MyFramework.OrderDirection.ASC, ucPager.PageIndex + 1, ucPager.PageSize, totalRecords)
            rptList.DataBind()
            ucPager.TotalRecords = totalRecords

        End If
    End Sub

End Class
